package com.hoperun.framework.ibatis.page;

import java.io.Serializable;
import java.util.List;

import lombok.Data;

/**
 * 对分页的基本数据进行一个简单的封装
 */
@Data
public class Page<T> implements Serializable {

	private static final long serialVersionUID = 1617649922003197011L;

	/***
	 * 当前页码的数据
	 */
	private List<T> rows;

	/***
	 * 总数据数
	 */
	private int total = 0;

	/***
	 * 总页码数
	 */
	private int totalPage = 0;

	/***
	 * 当前页码的数据
	 */
	private int records = 0;

	/**
	 * 当前页码
	 */
	private int page = 0;

	/**
	 * 每页数据信息 
	 */
	private int pageSize = 0;

	public Page() {
	}

	public Page(int records, int pageSize ,List<T> resultParam) {
		this.pageSize = pageSize ;
		setRows(resultParam);
		setRecords(records);
		setTotal(records);
	}

	public Page(int records, int page, int pageSize, List<T> resultParam) {

		int totalPage = records / pageSize;
		if (records % pageSize > 0) {
			totalPage += 1;
		}
		setTotal(totalPage);
		setRows(resultParam);
		setPage(page);
		setRecords(records);
	}

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		
		if (pageSize != 0) {
			if (total % pageSize == 0) {
				this.totalPage = total / pageSize;
			} else {
				this.totalPage = total / pageSize + 1;
			}
		}
		
		this.total = total;
	}

	public int getTotalPage() {

		if (pageSize != 0) {
			if (total % pageSize == 0) {
				this.totalPage = total / pageSize;
			} else {
				this.totalPage = total / pageSize + 1;
			}
		}

		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getRecords() {
		return records;
	}

	public void setRecords(int records) {
		this.records = records;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public String toString() {
		return "Page [rows=" + rows + ", total=" + total + ", totalPage="
				+ totalPage + ", records=" + records + ", page=" + page
				+ ", pageSize=" + pageSize + "]";
	}

}